// pages/check_list/check_list.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:applet_park_api_url_imgUrl,
    checktasknodeid: '',
    isLeader: false,
    nodeStatus: '',
    leaderConfirmed: false,
    hcchooseList: ["121"],
    disable: false,
    lists: [],
    code: '',
    checkTaskContents: [],
    foreignId: '',
    deviceClassificationId: '',
    checkTaskId: '',
    checkStandardId: '',
    platform: '',
    selected: 1,
    chooseList: [],
    listselected: '1',
    taskId:'',
    deviceId:'',
    taskName:'',
    status:'',
    list: [{
        name: '指示灯',
        id: 1,
        sublist: [{
            name: '指示灯正常',
            id: 222,
            status: false,
          },
          {
            name: '目视检查',
            id: 222,
            status: false,
          }
        ]
      },
      {
        name: '柜体及外观',
        id: 1,
        sublist: [{
            name: '无异味',
            id: 222,
            status: false,
          },
          {
            name: '无打火烧痕',
            id: 222,
            status: false,
          }
        ]
      }

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if(options.deviceId){
      this.setData({
        deviceId: options.deviceId
      })
    }
    if(options.taskId){
      this.setData({
        taskId: options.taskId
      })
    }
    if(options.taskName){
       this.setData({
        taskName: options.taskName
       })
    }
    if(options.status){
      this.setData({
        status: options.status,
        isLeader: options.isLeader
       })
    }
    
    this.loadData();
  },
  // 选择
  selectsataus: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.cid;
    
    var choosecheckStandardId = '';
    var n = 0;

    for (var i in that.data.lists) {
      var lista = that.data.lists[i];
      // 
      for (var j in lista.checkContentVos) {
        // 

        var pro = lista.checkContentVos;

        if (pro[j].id == id) {
          
          if (pro[j].contentStatus == 1) {
            pro[j].contentStatus = 2;
            
          } else {
            pro[j].contentStatus = 1;
          }
          let index = that.data.chooseList.findIndex((obj, i, objs) => {
            return obj.checkStandardId == lista.id && obj.checkContentId == pro[j].id
          });
          if (index > -1) {
             that.data.chooseList.splice(index,1);
          } else {
            that.data.chooseList.push({
              'checkStandardId': lista.id,
              'checkContentId': pro[j].id,
              'contentStatus': pro[j].contentStatus
            });
          }
        }
      }
    }
    that.data.checkStandardId = choosecheckStandardId;
    that.data.checkTaskContents = that.data.chooseList;
    that.setData(that.data);
    
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  selected: function(e) {
    var t = e.currentTarget.dataset.t;
    this.setData({
      selected: t
    })
  },
  listselected1: function(e) {
    var t = e.currentTarget.dataset.t;
    var index = e.currentTarget.dataset.index;
    var item = this.data.lists[index]
    item.open = !item.open;
    
    this.setData({
      listselected: t,
      navNumber: t
    })
    this.setData(this.data);
  },
  listselected: function(e) {
    
    
    var t = e.currentTarget.dataset.t;
    var index = e.currentTarget.dataset.index;
    var item = this.data.lists[index]
    item.open = !item.open;
    this.setData({
      listselected: 0,
      navNumber: 0
    });
    this.setData(this.data);
  },
  scan: function() {
    var that = this;
    wx.scanCode({
      success(res) {
        

        if (res.result == that.data.code) {
          that.setData({
            disable: true
          })
          wx.showToast({
            title: '扫码成功',
            icon: 'success'

          })
        } else {
          wx.showToast({
            title: '任务设备和当前设备不匹配，请选择正确设备',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail() {
        wx.showToast({
          title: '扫描失败',
          icon: 'fail',
          duration: 3000
        })
      }
    })
  },
  hqbtn() {
    var that = this;
    if (!that.data.disable) {
      wx.showToast({
        title: '请先扫码确认是否是任务设备',
        icon: 'none',
        duration: 3000
      })
      return
    }
    wx.showToast({
      title: '已缓存',
      icon: 'fail',
      duration: 2000
    });


  },
  submitPra: function() {//提交
    var that = this;
    // if (!that.data.chooseList.length > 0) {
    //   wx.showToast({
    //     title: '您尚未修改，请修改后保存',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return
    // }
    
    var arra = that.data.chooseList.reverse();
    var subIds = '';
    var status = '';
    
    for(var i=0;i<arra.length;i++){
      
        subIds += arra[i].checkContentId+',';
      
    }
    var data = {
      checkTaskId: subIds,
      taskId: that.data.taskId,
      deviceId: that.data.deviceId   
    }
    requestDataparams('/api/checkTasks/saveTaskContent', data, 'POST','json').then(res => {
      
      if (res.data.code == 0) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.loadData();
        prevPage.setData(prevPage.data);
        wx.navigateBack();
        // wx.redirectTo({
        //   url: '../patrol_record/patrol_record',
        // })

      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        });
      }
    }).catch(err => {
      
      that.data.hcchooseList.push(data);
      wx.setStorageSync('hcchooseList', that.data.hcchooseList);
      that.setData(that.data)
    });

  },
  submithc() {
    // var list = wx.getStorageSync('chooseList');
  },
  loadData() {
    var that = this;
    let data ={
      deviceId: that.data.deviceId,
      taskId: that.data.taskId
      };
    requestDataparams('/api/checkTasks/queryTaskDeviceContent',data, 'GET','json').then(res => {
      if (res.data.code == 0) {
        var lists = res.data.data;
        for (let i in lists) {
          lists[i].open = true;
        }
        that.setData({
          lists: lists
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    }).catch(err => {
      
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var hcchooseList = wx.getStorageSync('hcchooseList');
    this.data.hcchooseList = hcchooseList;
    this.setData(this.data);
  },
  hcsubmit() {

  },

  confirmnode() {
    var that = this;
    let url = '/api/checkTaskNodes/' + that.data.checktasknodeid + '/confirm';
    // wx.request({
    //   url: applet_park_api_url + '/api/checkTaskNodes/' + that.data.checktasknodeid + '/confirm',
    //   method: 'PUT',
    //   success: function(res) {
    //     )
    //     if (res.data.code == 0) {
    //       wx.showToast({
    //         title: '操作成功',
    //       })

    //       var pages = getCurrentPages();
    //       var prevPage = pages[pages.length - 2]; //上一个页面
    //       prevPage.loadData();

    //       prevPage.setData(prevPage.data);
    //       wx.navigateBack({

    //       })
    //     } else {
    //       wx.showToast({
    //         title: res.data.msg,
    //         icon: 'none'
    //       });
    //     }
    //   },
    //   fail: function(res) {
    //     )
    //   }

    // })

    requestDataparams(url, {}, 'PUT').then(res => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '操作成功',
        })

        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.loadData();

        prevPage.setData(prevPage.data);
        wx.navigateBack({

        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    }).catch(err => {
      
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})